package com.baomidou.springboot.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.springboot.common.SortEnum;
import com.baomidou.springboot.entity.News;
import com.baomidou.springboot.mapper.NewsMapper;
import com.baomidou.springboot.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 关于我们 服务实现类
 * </p>
 *
 * @author QQQ
 * @since 2018-06-19
 */
@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    private NewsMapper newsMapper;

    @Override
    public void save(News news) {
        news.setCreateDate(new Date());
        newsMapper.insert(news);
    }

    @Override
    public void delete(String id) {
        newsMapper.deleteById(id);
    }

    @Override
    public News edit(String id) {
        return newsMapper.selectById(id);
    }

    @Override
    public void update(News news) {
        news.setModifyDate(new Date());
        newsMapper.updateById(news);
    }

    @Override
    public News view(String id) {
        return newsMapper.selectById(id);
    }

    @Override
    public Page<News> list(Page<News> page, String search) {
        if (0 == page.getSize()) {
            page.setSize(10);
        }
        if (0 == page.getCurrent()) {
            page.setCurrent(1);
        }
        List<News> newsList = newsMapper.findByDateDesc(page, search);
        page.setRecords(newsList);
        return page;
    }

    @Override
    public void deleteBatch(List<String> ids) {
        if (null == ids) {
            return;
        }
        newsMapper.deleteBatchIds(ids);
    }

    @Override
    public List<News> findBySort(SortEnum sortEnum) {
        return newsMapper.findBySort(sortEnum.name());
    }

    @Override
    public List<News> findByLimit(Integer limit) {
        return newsMapper.findByLimit(limit);
    }

    @Override
    public List<News> findByMenuAndLimit(String menu, Integer limit) {
        return newsMapper.findByMenuAndLimit(menu, limit);
    }

    @Override
    public News findById(String id) {
        return newsMapper.selectById(id);
    }

    @Override
    public Page<News> findPage(Page<News> page, String menu) {
        if (0 == page.getSize()) {
            page.setSize(10);
        }
        if (0 == page.getCurrent()) {
            page.setCurrent(1);
        }
        List<News> newsList = newsMapper.findPage(page, menu);
        page.setRecords(newsList);
        return page;
    }
}
